﻿using System.IO;
using System.Threading.Tasks;
#if SILVERLIGHT
using System.Windows.Controls;
#else
using Microsoft.Win32;
#endif

namespace FreshMeat.Xaml
{
   public static class CommonDialogs
   {
      /// <summary>
      /// Allows user to pick an image through the appropriate means for the current OS.
      /// </summary>
      /// <param name="maxWidthOrHeight">If specified, image will be converted so it's largest dimension is this size while maintaining its original aspect ratio; if not specified, actual image size is used.</param>
      /// <returns></returns>
      public async static Task<MemoryStream> PicturePickerAsPng(int? maxWidthOrHeight)
      {
         var dialog = new OpenFileDialog();

#if SILVERLIGHT
         var result = dialog.ShowDialog();
#else
         var result = dialog.ShowDialog(VisualHelper.GetWindow());
#endif

         if (!result.GetValueOrDefault())
            return null;

#if SILVERLIGHT
         var file = dialog.File;
#else
         var file = new FileInfo(dialog.FileName);
#endif
         var data = await ImageHelper.ConvertToPng(file, maxWidthOrHeight, true);
         return new MemoryStream(data);
      }
   }
}
